﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading.Tasks;

namespace NetfractionHub.Network
{
	public class MessageQueueManager
	{
		public BlockingCollection<string> MessageQueue;
		public bool CanEnumerate { get; set; }

		public MessageQueueManager(Action<string> consumerCallback, int maxMessages = -1)
		{
			CanEnumerate = true;

			if (maxMessages == -1)
				MessageQueue = new BlockingCollection<string>();
			else
				MessageQueue = new BlockingCollection<string>(maxMessages);

			var consumer = Task.Factory.StartNew(() =>
				{
					foreach (var item in MessageQueue.GetConsumingEnumerable())
					{
						while (!CanEnumerate)
						{ System.Threading.Thread.Sleep(20); }


						consumerCallback(item);
					}
				});
		}
	}
}
